/**
 * 查询action类
 * @author 郑翎
 * 2015-10-1 11:07:43
 */
package com.cbts.action.info;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import com.cbts.dao.DepartmentDao;
import com.cbts.entity.ApplayReim;
import com.cbts.entity.BusinessTravelList;
import com.cbts.entity.CorporationMonthReport;
import com.cbts.entity.CorporationQuarterReport;
import com.cbts.entity.Department;
import com.cbts.entity.DepartmentMonthReport;
import com.cbts.entity.DepartmentQuarterReport;
import com.cbts.entity.ExchangeRecord;
import com.cbts.entity.Gift;
import com.cbts.entity.UserGroup;
import com.cbts.entity.UserInfo;
import com.cbts.service.info.SelectService;
import com.cbts.service.show.ReportService;
import com.cbts.util.ObjectJsonValueProcessor;
import com.cbts.util.Page;
import com.opensymphony.xwork2.ActionSupport;

public class SelectAction extends ActionSupport{

	private ReportService reportService;								//报表业务逻辑处理类
	private SelectService selectService;								//查询业务逻辑处理类
	private DepartmentDao departmentDao;								//部门数据处理类
	
	private String reporttype ;											//报表类型，0代表月报，1代表季报
	private String selectResult;										//用于前台传输数据的json结果串
	private String useridResult;										//用于前台传输数据的json结果串
	
	private List<Department> departments;								//获得全部的部门列表
	private Page<DepartmentMonthReport> departmentMonthReports;			//部门月报列表
	private Page<DepartmentQuarterReport> departmentQuarterReports;		//部门季报列表
	private Page<CorporationMonthReport> corporationMonthReports;		//公司月报列表
	private Page<CorporationQuarterReport> corporationQuarterReports;	//公司季报
	private Page<UserInfo> userInfos;									//员工信息列表
	private Page<BusinessTravelList> businessTravelLists;				//出差列表
	private Page<ApplayReim> applayReims;								//报销记录列表
	private Page<ExchangeRecord> exchangeRecords;						//兑换记录列表

	/**
	 * 选择类型,0-userinfo,1-traveinfo,2-applayinfo,3-exchangeinfo,4-depreport,5-correport
	 * 0-查询员工信息，1-查询出差记录，2-查询报销记录，3-查询兑换记录，4-查询部门月/季报
	 * 5-查询公司月/季报
	 * @return
	 */
	public String selectDepreport() {
		
		HttpServletRequest request = ServletActionContext.getRequest();		//声明请求
		String type=request.getParameter("type");							//获得参数
		JSONArray jsondata=new JSONArray();									//转换工具
		JSONArray depjsondata=new JSONArray();								//用于部门的转换工具
		JsonConfig depconfig=new JsonConfig(); 								//用于部门过滤配置
		
		selectResult = null;
		departments = departmentDao.findAll().getPageItems();				//获取全部部门信息
		depconfig.setExcludes(new String[]{"userInfos"						//过滤掉这三个属性
				,"departmentQuarterReports","departmentMonthReports"});
		depjsondata=JSONArray.fromObject(departments, depconfig);
		
		int page=1;//获得参数,选择查询的页数
		
		if(request.getParameter("page")!=null)
			page=Integer.parseInt(request.getParameter("page"));
		
		try {
			//按条件查询员工信息
			//如果部门id为空且userid为空，则查询全部员工信息
			//如果部门id为空，userid不为空，则按工号查询员工信息
			//如果部门id不为空，userid为空，则查询该部门的全部员工信息
			//如果部门id不为空，userid为空，则查询该部门的该员工信息
			if(type.compareTo("0")==0){
				
				StringBuffer buffer=new StringBuffer();
				JsonConfig config=new JsonConfig();
				
				buffer.append("{\"departmentlist\":").append(depjsondata.toString()); 		//封装查询条件——所有部门
				
				if (request.getParameter("departmentid") != null){
					
					if (request.getParameter("departmentid").toString()!=""){
						
							String key = new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
							String dename = new String( departmentDao.get(key).getDepartmentName());
							
							jsondata = JSONArray.fromObject("[\""+key+"\"]");
							buffer.append(",\"depid\":").append(jsondata.toString()); 		//查询的部门号
							jsondata = JSONArray.fromObject("[\""+dename+"\"]");
							buffer.append(",\"dename\":").append(jsondata.toString()); 		//查询的部门名称
							
						if (request.getParameter("searchuserid")==null||request.getParameter("searchuserid")=="") {
							
							userInfos = selectService.getuseInfos(key,null,1,page);				//查询某部门的全部员工
							
							
						}else {																//查询部门下的某员工信息
							
							String departmentid = new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
							String searchuserid = new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
							
							userInfos = selectService.getuseInfos(departmentid,searchuserid, 3,page);//查询部门下的某员工信息
							
							jsondata = JSONArray.fromObject("[\""+searchuserid+"\"]");
							buffer.append(",\"searchuserid\":").append(jsondata.toString()); 	//查询的员工号
						
						}
						
					}else {
						if (request.getParameter("searchuserid")!=null&&request.getParameter("searchuserid")!="") {
						
							String key = new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
							userInfos = selectService.getuseInfos(null,key, 2,page);					//按工号查询员工信息
							
							jsondata = JSONArray.fromObject("[\""+key+"\"]");
							buffer.append(",\"searchuserid\":").append(jsondata.toString()); 	//查询的员工号
						
							
						}else{
							userInfos = selectService.getuseInfos(null, null,0,page);				//获得所有员工
						}
					}
					
				}else {
					userInfos = selectService.getuseInfos(null, null,0,page);						//获得所有员工
				}
				
				Department department = new Department();
				UserGroup userGroup = new UserGroup();
				for (int i = 0; i < userInfos.getPageItems().size(); i++) {
					if (userInfos.getPageItems().get(i).getDepartment()==null) {
						
						userInfos.getPageItems().get(i).setDepartment(department);
					}
					if (userInfos.getPageItems().get(i).getUserGroup()==null) {
						
						userInfos.getPageItems().get(i).setUserGroup(userGroup);
					}
				}
				config.setExcludes(new String[]{"registerTime"			//过滤属性
						,"businessTravels","businessTravelListsForUserid"
						,"businessTravelListsForVerifierId"
						,"applayReims","exchangeRecords"});
				config.registerJsonValueProcessor(Department.class,		//保留部门id和名称 
						new ObjectJsonValueProcessor(new String[]{"departmentId","departmentName"},
								Department.class));	
				config.registerJsonValueProcessor(UserGroup.class,		//保留用户组id和名称
						new ObjectJsonValueProcessor(new String[]{"userGroupId","userGroupName"},
								UserGroup.class));
				
				buffer.append(",\"pagenum\":").append(userInfos.getPageNo());
				buffer.append(",\"pages\":").append((userInfos.getTotalPageNum()));
				jsondata=JSONArray.fromObject(userInfos.getPageItems(),config);
				buffer.append(",\"userlist\":").append(jsondata.toString()).append("}");
				
				selectResult=buffer.toString();
				
			}else if(type.compareTo("1")==0){
				
				//按条件出差记录
				//如果部门id为""且状态为""且userid为空，则查询全部出差记录
				//如果部门id为null且状态为null且userid为空，则查询全部出差记录
				//部门、状态和userid排列组合查询出差记录
				
				StringBuffer buffer=new StringBuffer();
				JsonConfig config=new JsonConfig();
				String[] status = {"","待审核","审核不通过","审核通过","待报销","已报销","报销不通过"};
				List<String> verNames = new ArrayList<String>();
				
				buffer.append("{\"departmentlist\":").append(depjsondata.toString());
				
				if (request.getParameter("departmentid") != null&&request.getParameter("morq")!=null
						&&request.getParameter("searchuserid")!=null)
				{
					int morq = Integer.parseInt(request.getParameter("morq").toString());
					String deid = "";
					String dename = "";
					
					if (request.getParameter("departmentid")!="") {
						
						 deid =new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
						 dename = departmentDao.get(deid).getDepartmentName().toString();
						 jsondata = JSONArray.fromObject("[\""+deid+"\"]");
						 buffer.append(",\"depid\":").append(jsondata.toString()); 		//查询的部门号
						 jsondata = JSONArray.fromObject("[\""+dename+"\"]");
							buffer.append(",\"dename\":").append(jsondata.toString()); 	//查询的部门名称
							
					}
					
					String userid =new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
					String[] conditions = new String[4];
					conditions[0] = "";
					conditions[1] = deid;
					conditions[2] = userid;
					conditions[3] = status[morq];
					
					businessTravelLists = selectService.getbBusinessTravels(conditions,page);//部门、状态和userid排列组合查询出差记录
					
					jsondata = JSONArray.fromObject("[\""+userid+"\"]");
					buffer.append(",\"searchuserid\":").append(jsondata.toString()); 	//查询的员工号
					
					jsondata = JSONArray.fromObject("[\""+morq+"\"]");
					buffer.append(",\"morq\":").append(jsondata.toString()); 			//查询的状态
					
				}else {
					
					businessTravelLists = selectService.getbBusinessTravels(null,page);		 //获得所有出差列表
					
				}
				
				if(request.getParameter("departmentid") == null||request.getParameter("departmentid") == ""){
					
					List<String> depidStrings = new ArrayList<String>();
					
					for (int i = 0; i < businessTravelLists.getPageItems().size(); i++) {				//封装部门信息
						
						depidStrings.add(businessTravelLists.getPageItems().get(i)
								.getUserInfoByUserid().getDepartment().getDepartmentName());
						jsondata = JSONArray.fromObject(depidStrings);
						buffer.append(",\"depidStrings\":").append(jsondata.toString());
						
					}
				}
				
				config.setExcludes(new String[]{"applayReim","businessTravels","userInfoByVerifierId"}); //过滤属性
				config.registerJsonValueProcessor(UserInfo.class,
						new ObjectJsonValueProcessor(new String[]{"userid"	   	 	 //保留员工号和员工名
								,"userName"},UserInfo.class));	
				
				for (int i = 0; i < businessTravelLists.getPageItems().size(); i++) {
					if(businessTravelLists.getPageItems().get(i).getUserInfoByVerifierId()!=null)
						verNames.add(businessTravelLists.getPageItems().get(i).getUserInfoByVerifierId().getUserName());
					else
						verNames.add("");
				}
				
				jsondata = JSONArray.fromObject(verNames);
				buffer.append(",\"vernames\":").append(jsondata.toString());	//转换为string
				buffer.append(",\"pagenum\":").append(businessTravelLists.getPageNo());
				buffer.append(",\"pages\":").append((businessTravelLists.getTotalPageNum()));
				jsondata=JSONArray.fromObject(businessTravelLists.getPageItems(),config);
				buffer.append(",\"travellist\":").append(jsondata.toString()).append("}");
				selectResult=buffer.toString();
				
			}else if(type.compareTo("2")==0){  
				
				//按条件报销记录
				//如果部门id为""且状态为""且userid为空，则查询全部报销记录
				//如果部门id为null且状态为null且userid为空，则查询全部报销记录
				//部门、状态和userid排列组合查询出差记录
				
				StringBuffer buffer=new StringBuffer();
				JsonConfig config=new JsonConfig();
				JsonConfig bConfig = new JsonConfig();
				String[] status = {"","待报销","已报销","报销不通过"};
				
				buffer.append("{\"departmentlist\":").append(depjsondata.toString());
				
				if (request.getParameter("departmentid") != null&&request.getParameter("morq")!=null
						&&request.getParameter("searchuserid")!=null){
					
					int morq = Integer.parseInt(request.getParameter("morq").toString());
					String deid = "";
					String dename = "";
					
					if (request.getParameter("departmentid")!="") {
						
						 deid =new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
						 dename = departmentDao.get(deid).getDepartmentName().toString();
						 jsondata = JSONArray.fromObject("[\""+deid+"\"]");
						 buffer.append(",\"depid\":").append(jsondata.toString()); 		//查询的部门号
						 jsondata = JSONArray.fromObject("[\""+dename+"\"]");
							buffer.append(",\"dename\":").append(jsondata.toString());  //查询的部门名称
							
					}
					
					String userid =new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
					String[] conditions = new String[4];
					conditions[0] = "";
					conditions[1] = deid;
					conditions[2] = userid;
					conditions[3] = status[morq];
					
					applayReims = selectService.getApplayReims(conditions,page);				//部门、状态和userid排列组合查询出差记录
					
					jsondata = JSONArray.fromObject("[\""+userid+"\"]");
					buffer.append(",\"searchuserid\":").append(jsondata.toString()); 	//查询的员工号
					
					jsondata = JSONArray.fromObject("[\""+morq+"\"]");
					buffer.append(",\"morq\":").append(jsondata.toString()); 			//查询的状态
					
					
				}else{
					applayReims = selectService.getApplayReims(null,page);					//获得所有报销记录			
				}
				
				if(request.getParameter("departmentid") == null||request.getParameter("departmentid") == ""){
					List<String> depidStrings = new ArrayList<String>();
					for (int i = 0; i < applayReims.getPageItems().size(); i++) {
						
						depidStrings.add(applayReims.getPageItems().get(i).getBusinessTravelList()
								.getUserInfoByUserid().getDepartment().getDepartmentName());
						jsondata = JSONArray.fromObject(depidStrings);
						buffer.append(",\"depidStrings\":").append(jsondata.toString());
					}
				}
				
				businessTravelLists.setPageItems(new ArrayList<BusinessTravelList>());
				
				for (int i = 0; i < applayReims.getPageItems().size(); i++) {
					businessTravelLists.getPageItems().add(applayReims.getPageItems().get(i).getBusinessTravelList());
				}
				
				bConfig.setExcludes(new String[]{"applayReim","businessTravels"});	//过滤这两个属性
				bConfig.registerJsonValueProcessor(UserInfo.class,					//保留员工号和员工名
						new ObjectJsonValueProcessor(new String[]{"userid"
								,"userName"},UserInfo.class));	
				
				jsondata=JSONArray.fromObject(businessTravelLists.getPageItems(),bConfig);
				buffer.append(",\"appbuslist\":").append(jsondata.toString());
				
				config.registerJsonValueProcessor(UserInfo.class,					//保留员工号和员工名
						new ObjectJsonValueProcessor(new String[]{"userid"
								,"userName"},UserInfo.class));	
				config.registerJsonValueProcessor(BusinessTravelList.class,			//保留出差列表的状态
						new ObjectJsonValueProcessor(new String[]{"verifierStatus"},
								BusinessTravelList.class));
				
				
				buffer.append(",\"pagenum\":").append(applayReims.getPageNo());
				buffer.append(",\"pages\":").append((applayReims.getTotalPageNum()));
				jsondata=JSONArray.fromObject(applayReims.getPageItems(),config);
				buffer.append(",\"applaylist\":").append(jsondata.toString()).append("}");
				
				selectResult=buffer.toString();
				
			}else if(type.compareTo("3")==0){  
				
				//按条件查询兑换记录
				//部门、领取状态和员工三种查询条件组合查询
				
				StringBuffer buffer=new StringBuffer();
				JsonConfig config=new JsonConfig();
				String[] status = {"否","是"};
				
				buffer.append("{\"departmentlist\":").append(depjsondata.toString());
				
				if (request.getParameter("departmentid") != null&&request.getParameter("morq")!=null
						&&request.getParameter("searchuserid")!=null)
				{
					int morq = Integer.parseInt(request.getParameter("morq").toString());
					String deid = "";
					String dename = "";
					
					if (request.getParameter("departmentid")!="") {
						
						 deid =new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
						 dename = departmentDao.get(deid).getDepartmentName().toString();
						 jsondata = JSONArray.fromObject("[\""+deid+"\"]");
						 buffer.append(",\"depid\":").append(jsondata.toString()); 		//查询的部门号
						 jsondata = JSONArray.fromObject("[\""+dename+"\"]");
							buffer.append(",\"dename\":").append(jsondata.toString()); 	//查询的部门名称
					}
					
					String userid =new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
					String[] conditions = new String[4];
					
					conditions[0] = "";
					conditions[1] = deid;
					conditions[2] = userid;
					conditions[3] = status[morq];
					
					exchangeRecords = selectService.getExchangeRecords(conditions,page);		//部门、领取状态和员工三种查询条件组合查询
					
					jsondata = JSONArray.fromObject("[\""+userid+"\"]");
					buffer.append(",\"searchuserid\":").append(jsondata.toString()); 	//查询的员工号
					
					jsondata = JSONArray.fromObject("[\""+morq+"\"]");
					buffer.append(",\"morq\":").append(jsondata.toString()); 			//查询的状态
					
				}else {
					exchangeRecords = selectService.getExchangeRecords(null,page);	//获得所有交换记录
				}
				if(request.getParameter("departmentid") == null||request.getParameter("departmentid") == ""){
					List<String> depidStrings = new ArrayList<String>();
					for (int i = 0; i < exchangeRecords.getPageItems().size(); i++) {
						
						depidStrings.add(exchangeRecords.getPageItems().get(i).getUserInfo()
								.getDepartment().getDepartmentName());
						
						jsondata = JSONArray.fromObject(depidStrings);
						buffer.append(",\"depidStrings\":").append(jsondata.toString());
						
					}
				}
				
				
				config.registerJsonValueProcessor(UserInfo.class,
						new ObjectJsonValueProcessor(new String[]{"userid"	//保留员工号和员工名
								,"userName"},UserInfo.class));	

				config.registerJsonValueProcessor(Gift.class,				//保留礼品名、礼品价值和礼品描述
						new ObjectJsonValueProcessor(new String[]{"giftName"
								,"giftValue","description"},Gift.class));
				
				buffer.append(",\"pagenum\":").append(exchangeRecords.getPageNo());
				buffer.append(",\"pages\":").append((exchangeRecords.getTotalPageNum()));
				jsondata=JSONArray.fromObject(exchangeRecords.getPageItems(),config);
				buffer.append(",\"exchangelist\":").append(jsondata.toString()).append("}");
				
				selectResult=buffer.toString();
				
			}else if(type.compareTo("4")==0){
				
				//按条件查询部门月/季报
				//部门、报表类型、年份三种条件组合查询部门月/季报
				
				StringBuffer buffer=new StringBuffer();
				JsonConfig config=new JsonConfig();
				JsonConfig tsconfig=new JsonConfig();
				String years = "";
				
				tsconfig.setExcludes(new String[]{"userInfos"					//过滤属性
						,"departmentQuarterReports","departmentMonthReports"});
				buffer.append("{\"departmentlist\":").append(depjsondata.toString());
				
				if (request.getParameter("departmentid") != null&&request.getParameter("morq")!=null)//得到部门id
				{
					String deid = null;
					String dename = "";
					
					if (request.getParameter("departmentid")!="") {
						
						deid =new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
						 dename = departmentDao.get(deid).getDepartmentName().toString();
						 
						 jsondata = JSONArray.fromObject("[\""+deid+"\"]");
						 buffer.append(",\"depid\":").append(jsondata.toString()); 		//查询的部门号
						 jsondata = JSONArray.fromObject("[\""+dename+"\"]");
							buffer.append(",\"dename\":").append(jsondata.toString()); 	//查询的部门名称
					}
					
					if (request.getParameter("morq").equals("0")) {						//查询部门月报
						
						buffer.append(",\"morq\":").append("[\"0\"]");			 	  //显示月报or季报，0 or 1
						
						if (request.getParameter("departmentid").toString()!=""){	  //查询某部门月报

							if (request.getParameter("searchuserid")!=null 			  //查询某部门某年的月报
									&&request.getParameter("searchuserid")!="") {
								
								years = new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
								departmentMonthReports = selectService.getDeMonthReport(deid,years,page);
								
							}else {//查询某部门全部的月报
								departmentMonthReports = selectService.getDeMonthReport(deid,null,page);							
							}
							
						}else {
							
							if (request.getParameter("searchuserid")!=null 				//查询全部部门某年的月报
									&&request.getParameter("searchuserid")!="") {
								
								years = new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
								departmentMonthReports = selectService.getDeMonthReport("",years,page);
								
							}else {					
								departmentMonthReports = selectService.finadAllDepMon(page);	//获得全部的部门月报					
							}
						}
						
						buffer.append(",\"years\":").append("[\""+years+"\"]");	 			//查询的年份
						config.registerJsonValueProcessor(Department.class,
								new ObjectJsonValueProcessor(new String[]{"departmentId" 	//保留部门id和名称 
										,"departmentName"},Department.class));
						
						buffer.append(",\"pagenum\":").append(departmentMonthReports.getPageNo());
						buffer.append(",\"pages\":").append((departmentMonthReports.getTotalPageNum()));
						jsondata=JSONArray.fromObject(departmentMonthReports.getPageItems(),config);
						buffer.append(",\"depreportlist\":").append(jsondata.toString()).append("}");
						
					}else {//查询部门季报
						
						buffer.append(",\"morq\":").append("[\"1\"]");			   	//显示月报or季报，0 or 1
						
						if (request.getParameter("departmentid").toString()!=""){	//查询某部门季报

							if (request.getParameter("searchuserid")!=null 			//查询某部门某年的季报
									&&request.getParameter("searchuserid")!="") {
								
								years = new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
								departmentQuarterReports = selectService.getDeQuarterReport(deid,years,page);
								
							}else {//查询某部门全部的季报
								departmentQuarterReports = selectService.getDeQuarterReport(deid,null,page);							
							}
							
						}else {//查询全部部门的某年的季报
							
							if (request.getParameter("searchuserid")!=null //查询全部部门某年的季报
									&&request.getParameter("searchuserid")!="") {
								
								years = new String((request.getParameter("searchuserid")).getBytes("ISO-8859-1"),"utf-8");
								departmentQuarterReports = selectService.getDeQuarterReport("",years,page);
								
							}else {//获得全部的部门季报
								departmentQuarterReports = selectService.finadAllDepQua(page);							
							}
						}
						
						buffer.append(",\"years\":").append("[\""+years+"\"]");	 			//查询的年份
						config.registerJsonValueProcessor(Department.class,
								new ObjectJsonValueProcessor(new String[]{"departmentId"	 //保留部门id和名称 
										,"departmentName"},Department.class));
						
						buffer.append(",\"pagenum\":").append(departmentQuarterReports.getPageNo());
						buffer.append(",\"pages\":").append((departmentQuarterReports.getTotalPageNum()));
						jsondata=JSONArray.fromObject(departmentQuarterReports.getPageItems(),config);
						buffer.append(",\"depreportlist\":").append(jsondata.toString()).append("}");
					}
					
					
				}else {//显示所有部门的月报
					
					buffer.append(",\"morq\":").append("[\"0\"]");			 				  //显示月报or季报，0 or 1
					
					departmentMonthReports = selectService.finadAllDepMon(page);				  //获得全部的部门月报
					
					config.registerJsonValueProcessor(Department.class,
							new ObjectJsonValueProcessor(new String[]{"departmentId" 		 //保留部门id和名称 
									,"departmentName"},Department.class));
					
					buffer.append(",\"pagenum\":").append(departmentMonthReports.getPageNo());
					buffer.append(",\"pages\":").append((departmentMonthReports.getTotalPageNum()));
					jsondata=JSONArray.fromObject(departmentMonthReports.getPageItems(),config);
					buffer.append(",\"depreportlist\":").append(jsondata.toString()).append("}");
					
				}
				
				selectResult=buffer.toString();
				
			}else if(type.compareTo("5")==0){
				
				//按条件查询公司的月/季报
				//报表类型和年份两种条件组合查询公司的月/季报
				
				StringBuffer buffer=new StringBuffer();
				String years = "";
				
				if (request.getParameter("morq")!=null&&request.getParameter("morq")!="") {
					
					if (request.getParameter("morq").equals("0")) {						//查询公司月报
						
						if (request.getParameter("searchuserid")!=null 					//查询公司某年的月报
								&&request.getParameter("searchuserid")!="") {
							
							 years = request.getParameter("searchuserid").toString();
							corporationMonthReports = selectService.getCoMonthReport(years,page);
							
						}else {//查询公司全部的月报
							corporationMonthReports = selectService.finadAllCorMon(page);	//获得所有公司报表
						}
						
						buffer.append("{\"morq\":").append("[\"0\"]");					//显示月报or季报，0 or 1
						buffer.append(",\"years\":").append("[\""+years+"\"]");	 		//查询的年份
						
						buffer.append(",\"pagenum\":").append(corporationMonthReports.getPageNo());
						buffer.append(",\"pages\":").append((corporationMonthReports.getTotalPageNum()));
						jsondata=JSONArray.fromObject(corporationMonthReports.getPageItems());
						buffer.append(",\"correportlist\":").append(jsondata.toString()).append("}");
						
					}else {//查询公司季报
						
						if (request.getParameter("searchuserid")!=null 					//查询公司某年的季报
								&&request.getParameter("searchuserid")!="") {
							
							years = request.getParameter("searchuserid").toString();
							corporationQuarterReports = selectService.getCoQuarterReport(years,page);
							
						}else {//获得所有公司季报表
							corporationQuarterReports = selectService.finadAllCorQua(page);	//获得所有公司季报表
						}
						
						buffer.append("{\"morq\":").append("[\"1\"]");					//显示月报or季报，0 or 1
						buffer.append(",\"years\":").append("[\""+years+"\"]");	 		//查询的年份
						
						buffer.append(",\"pagenum\":").append(corporationQuarterReports.getPageNo());
						buffer.append(",\"pages\":").append((corporationQuarterReports.getTotalPageNum()));
						jsondata=JSONArray.fromObject(corporationQuarterReports.getPageItems());
						buffer.append(",\"correportlist\":").append(jsondata.toString()).append("}");
						
					}
				}else {
					
					corporationMonthReports = selectService.finadAllCorMon(page);			//获得所有公司月报表
					buffer.append("{\"morq\":").append("[\"0\"]");						//显示月报or季报，0 or 1
					
					buffer.append(",\"pagenum\":").append(corporationMonthReports.getPageNo());
					buffer.append(",\"pages\":").append((corporationMonthReports.getTotalPageNum()));
					jsondata=JSONArray.fromObject(corporationMonthReports.getPageItems());
					buffer.append(",\"correportlist\":").append(jsondata.toString()).append("}");
				}
				
				selectResult=buffer.toString();
				
			}
		
			return "selallinfo";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "selallinfo";
	}
	
	/**
	 * 搜索的下拉框显示
	 * 根据部门动态获取员工号,根据type判断是报表还是其他查询
	 * 若不是报表查询，则封装useridresult并封装yearsresult为null
	 * 若是报表查询则封装yearsresult并封装useridresult为null
	 * @return
	 */
	public String getdepartments() {
		
		StringBuffer buffer=new StringBuffer();
		JSONArray jsondata=new JSONArray();										//转换工具
		List<UserInfo> us = new ArrayList<UserInfo>();
		List<String> ds = new ArrayList<String>();
		List<String> years = new ArrayList<String>();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		int type = Integer.parseInt(request.getParameter("type").toString());
		useridResult = null;
		
		try {
			if (type<4) {
				
				if (request.getParameter("departmentid") != null)				//得到部门id
				{
					if (request.getParameter("departmentid").toString()=="") {	//获得所有员工的工号
						us = selectService.getuseInfos(null, null,0,1).getPageItems();
					}else {//查询某部门的全部员工工号
						
						String key = new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
						
						us = selectService.getuseInfos(key,null, 1,1).getPageItems();
					}
				}
				if (request.getParameter("search-text") != null) {
				
					String key = new String((request.getParameter("search-text")).getBytes("ISO-8859-1"),"utf-8");
					
					if (key.length() != 0) {
						
						int s=0;
						
						for (int i = 0; i < us.size(); i++) {
							
							String temp = us.get(i).getUserid();
							
							if(s>4)//最多显示5条
								break;
							
							if (temp.startsWith(key)) {
								ds.add(temp);
								s++;
							}
						}

					}else{
						for (int i = 0; i < us.size(); i++) {
							ds.add(us.get(i).getUserid());
						}
					}				
				}
				
				jsondata = JSONArray.fromObject(ds);
				buffer.append("{\"useridresult\":").append(jsondata.toString()).append("}");	//转换为string
				useridResult = buffer.toString();
				
			}else if(type == 4){
				
				int morq = Integer.parseInt(request.getParameter("morq").toString());
				
				if (request.getParameter("departmentid") != null)				//得到部门id
				{
					if (request.getParameter("departmentid").toString()=="") {	//获得所有部门月/季报的年份
						years = selectService.getYear(morq, "");
					}else {//查询某部门的月/季报的年份
						
						String key = new String((request.getParameter("departmentid")).getBytes("ISO-8859-1"),"utf-8");
						years = selectService.getYear(morq, key);
						
					}
				}
				if (request.getParameter("search-text") != null) {
					
					String key = new String((request.getParameter("search-text")).getBytes("ISO-8859-1"),"utf-8");
					
					if (key.length() != 0) {
						
						int s=0;
						
						for (int i = 0; i < years.size(); i++) {
							
							String temp = years.get(i);
							
							if(s>4)//最多显示5条
									break;
							
							if (temp.startsWith(key)) {
								ds.add(temp);
								s++;
							}
						}

					}else{
						for (int i = 0; i < years.size(); i++) {
							ds.add(years.get(i));
						}
					}
				
				}
				jsondata = JSONArray.fromObject(ds);
				buffer.append("{\"useridresult\":").append(jsondata.toString()).append("}");	//转换为string
				useridResult = buffer.toString();
				
			}else {
				
				int morq = Integer.parseInt(request.getParameter("morq").toString());
				years = selectService.getYear(morq, null);//获得公司所有月/季报的年份
				
				if (request.getParameter("search-text") != null) {
					
					String key = new String((request.getParameter("search-text")).getBytes("ISO-8859-1"),"utf-8");
					
					if (key.length() != 0) {
						
						int s=0;
						
						for (int i = 0; i < years.size(); i++) {
							
							String temp = years.get(i);
							
							if(s>4)//最多显示5条
								break;
							
							if (temp.startsWith(key)) {
								ds.add(temp);
								s++;
							}
						}

					}else{
						for (int i = 0; i < years.size(); i++) {
							ds.add(years.get(i));
						}
					}
				
				}
				
				jsondata = JSONArray.fromObject(ds);
				buffer.append("{\"useridresult\":").append(jsondata.toString()).append("}");	//转换为string
				useridResult = buffer.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "useridresult";
	}
	
	public SelectService getSelectService() {
		return selectService;
	}
	public void setSelectService(SelectService selectService) {
		this.selectService = selectService;
	}


	public ReportService getReportService() {
		return reportService;
	}


	public void setReportService(ReportService reportService) {
		this.reportService = reportService;
	}


	public String getReporttype() {
		return reporttype;
	}


	public void setReporttype(String reporttype) {
		this.reporttype = reporttype;
	}


	public String getSelectResult() {
		return selectResult;
	}


	public void setSelectResult(String selectResult) {
		this.selectResult = selectResult;
	}
	public String getUseridResult() {
		return useridResult;
	}
	public void setUseridResult(String useridResult) {
		this.useridResult = useridResult;
	}
	public DepartmentDao getDepartmentDao() {
		return departmentDao;
	}
	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}
}
